<template>
  <div class="dashboard-container" >
    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="peoples" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              学生人数
            </div>
            <count-to :start-val="0" :end-val=this.listInfo.studentNum :duration="2600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="peoples" class-name="card-panel-icon" style="color: #36a3f7"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              老师人数
            </div>
            <count-to :start-val="0" :end-val=this.listInfo.teacherNum :duration="2600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
<!--      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">-->
<!--        <div class="card-panel" >-->
<!--          <div class="card-panel-icon-wrapper icon-message">-->
<!--            <svg-icon icon-class="message" class-name="card-panel-icon" />-->
<!--          </div>-->
<!--          <div class="card-panel-description">-->
<!--            <div class="card-panel-text">-->
<!--              Messages-->
<!--            </div>-->
<!--            <count-to :start-val="0" :end-val="81212" :duration="3000" class="card-panel-num" />-->
<!--          </div>-->
<!--        </div>-->
<!--      </el-col>-->
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon icon-class="tree" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              班级数量
            </div>
            <count-to :start-val="0" :end-val=this.listInfo.classNum :duration="3200" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-shopping">
            <svg-icon icon-class="excel" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              科目数量
            </div>
            <count-to :start-val="0" :end-val=this.listInfo.subjectNum :duration="3600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :xs="24" :sm="24" :lg="12">
        <div id="main" style="width: 100%;height:500px"></div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div id="main2" style="width: 100%;height:500px"></div>
      </el-col>

    </el-row>


  </div>
</template>

<script>
import CountTo from 'vue-count-to'
import { getSexNum, getYearAllStudent, info } from '@/api/dashboard'

export default {
  components: {
    CountTo
  },
  data() {
    return {
      listInfo:{
        studentNum: 0,
        teacherNum: 0,
        classNum: 0,
        subjectNum: 0,
      },
      yearAllStudent:[
        {
          year:"",
          studentNum: "",
        },
      ],
      //echarts
      year:[],
      studentNum: [],
      sexNum:[],

      option : {
        visualMap: [
          {
            show: false,
            type: 'continuous',
            seriesIndex: 0,
          },

        ],
        title: {
          left: 'center',
          text: '每学年的学生人数'
        },
        tooltip: {
          trigger: 'axis'
        },

        xAxis: {
          type: 'category',
          data: [123,123]
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [1234,1234],
            type: 'line'
          }
        ]
      },
      option2: {
        title: {
          text: '学校学生男女比例图',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '学校学生男女比例',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 1048, name: '男生' },
              { value: 735, name: '女生' },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }

    };
  },
  created() {
      this.getInfo()
      // this.getYearAllStudent()
  },
  mounted() {
    this.getYearAllStudent()
    this.getSexNum()
  },
  watch: {
    year: function (val){
      this.$nextTick(()=>{
        this.drawLine()
      })
    },
    sexNum: function(val){
      this.$nextTick(()=>{
        this.drawLine()
        console.log()
      })
    },
  },
  methods: {
    getYearAllStudent(){
      getYearAllStudent().then(res => {
        this.yearAllStudent = res.data.data
        this.yearAllStudent.forEach(y => {
          this.year.push(y.year)
          this.studentNum.push(y.studentNum)
        })
        this.option.series[0].data = this.studentNum
        this.option.xAxis.data = this.year
      })
    },
    getSexNum(){
      getSexNum().then(res => {
        const sexNum = res.data.data
        for (let i = 0; i < sexNum.length; i++) {
          const sexNumTemp = {value:"",name:""}
          if (sexNum[i].sex === 1){
            sexNumTemp.name = '男'
          }else if(sexNum[i].sex === 2){
            sexNumTemp.name = '女'
          }
          sexNumTemp.value = sexNum[i].num
          this.sexNum.push(sexNumTemp)
        }
        this.option2.series[0].data = this.sexNum
      })
    },
    getInfo(){
      info().then(res => {
        this.listInfo = res.data.data
      })
    },
    handleSetLineChartData(type) {
      this.$emit('handleSetLineChartData', type)
    },
    drawLine() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = this.$echarts.init(document.getElementById('main'));
      var myChart2 = this.$echarts.init(document.getElementById('main2'));
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(this.option);
      myChart2.setOption(this.option2);

    }
  }
};
</script>

<style lang="scss" scoped>
html{
  background-color: rgb(240,242,245)
}
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    //&:hover {
    //  .card-panel-icon-wrapper {
    //    color: #fff;
    //  }
    //
    //  .icon-people {
    //    background: #40c9c6;
    //  }
    //
    //  .icon-message {
    //    background: #36a3f7;
    //  }
    //
    //  .icon-money {
    //    background: #f4516c;
    //  }
    //
    //  .icon-shopping {
    //    background: #34bfa3
    //  }
    //}

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
